package com.lixinyu.controller;

import com.lixinyu.pojo.ResponseMessage;
import com.lixinyu.pojo.User;
import com.lixinyu.pojo.dto.UserDto;
import com.lixinyu.pojo.dto.UserUpdateDto;
import com.lixinyu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    //增加
    @PostMapping
    public ResponseMessage add(@Validated @RequestBody UserDto user){
        User userAdd = userService.add(user);
        return ResponseMessage.success(userAdd);
    }
    //查询
    @GetMapping("/{userId}")
    public ResponseMessage select(@PathVariable Integer userId){
        User userAdd = userService.getUser(userId);
        return ResponseMessage.success(userAdd);
    }
    //修改
    @PutMapping
    public ResponseMessage update(@Validated @RequestBody UserUpdateDto user){
        User userAdd = userService.update(user);
        return ResponseMessage.success(userAdd);
    }

    //删除
    @DeleteMapping("/{userId}")
    public ResponseMessage delete(@PathVariable Integer userId){
        userService.delete(userId);
        return ResponseMessage.success();

    }

}
